﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace CWindow
{
    /// <summary>
    /// 窗口类
    /// </summary>
    public  class CWindow
    {

       
        #region win 消息代码
        public static   uint WM_ClOSE = 0x0010;
        public static   uint WM_KEYDOWN = 0x0100;
        public static   uint WM_KEYUP = 0x0101;
        public static   uint WM_CHAR = 0x0102;
        public static   uint WM_NCACTIVE = 0x0086;
        public static   uint WM_PASTE = 0x0302;
        #endregion
     
        /// <summary>
        /// 标题
        /// </summary>
        public string Caption { get; set; }

        /// <summary>
        /// 句柄
        /// </summary>
        public IntPtr Hwnd { get; set; }

        public string ClassName { get; set; }

        public CWindow(IntPtr windowHwnd, string caption,string className)
        {
            Caption = caption;
            Hwnd = windowHwnd;
            ClassName = className;
        }

        public CWindow(IntPtr windowHwnd, string caption)
        {
            Caption = caption;
            Hwnd = windowHwnd;
        }

        #region 窗口操作——消息
        /// <summary>
        /// 显示窗口
        /// </summary>
        public void ShowWindow()
        {
         if(! Hwnd.Equals(win32API.win32API.GetForegroundWindow()))
         {
            //激活并显示窗口
            win32API.win32API.SetForegroundWindow(Hwnd);
            win32API.win32API.ShowWindow(Hwnd, 1);
         }
        }

        /// <summary>
        /// 虚拟按键down
        /// </summary>
        /// <param name="wParam"></param>
        /// <param name="lParam"></param>
        public void SendVirtralKeyDown(uint wParam,uint lParam ) {
            win32API.win32API.PostMessage(Hwnd, WM_KEYDOWN, wParam, lParam);
        }

        /// <summary>
        /// 虚拟按键up
        /// </summary>
        /// <param name="wParam"></param>
        /// <param name="lParam"></param>
        public void SendVirtralKeyUp(uint wParam, uint lParam)
        {
            win32API.win32API.PostMessage(Hwnd, WM_KEYUP, wParam, lParam);
        }

        /// <summary>
        /// 发送字符
        /// </summary>
        /// <param name="wParam"></param>
        /// <param name="lParam"></param>
        public void SendChar(uint wParam, uint lParam)
        {
            win32API.win32API.PostMessage(Hwnd, WM_CHAR, wParam, lParam);
        }

        /// <summary>
        /// 关闭窗口
        /// </summary>
        public void CloseWindow()
        {
            win32API.win32API.PostMessage(Hwnd, WM_ClOSE, IntPtr.Zero, IntPtr.Zero);
        }

        #endregion

        #region 窗口操作——模拟按键
        /// <summary>
        /// 模拟按键复制
        /// </summary>
        public void AnalogPaste()
        {
            win32API.win32API.keybd_event((byte)Keys.ControlKey, 0, 0, 0);//CTRL 按下
            win32API.win32API.keybd_event((byte)Keys.V, 0, 0, 0);//V 按下
            win32API.win32API.keybd_event((byte)Keys.ControlKey, 0, 2, 0);//CTRL 释放
            win32API.win32API.keybd_event((byte)Keys.V, 0, 2, 0);//V 释放
        }

        
        #endregion

    }
}
